package com.ebt.m.customer.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.ebt.m.AppContext;
import com.ebt.m.customer.util.RecordManager;
import com.ebt.m.customer.util.TapeViewUtil;
import com.ebt.m.data.bean.BeanCustomerTape;

import java.util.List;

/**
 * created by rick.wang
 */
public class TapeMessageContainerView extends LinearLayout{

    private RecordManager mRecordManager;
    private RecorderWrapper mRecorderWrapper;

    public TapeMessageContainerView(Context context) {
        super(context);
    }
    public TapeMessageContainerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TapeMessageContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    TapeMessageView.TapeOperate mCallBack;
    public void setTapeMessageViewCallback(TapeMessageView.TapeOperate callBack){
        mCallBack = callBack;
    }

    public void update(String noteUuid){
        if(mRecordManager == null){
            mRecordManager = RecordManager.getInstance().setAppContext((AppContext)
                    (getContext().getApplicationContext()));
        }
        if(TextUtils.isEmpty(noteUuid)){
            Log.d("TapeMessageContainer", "noteUuid in TapeMessageContainerView, noteUuid is empty!");
        }
        List<BeanCustomerTape> tapes = mRecordManager.getBeanCustomerTapes(noteUuid);

        if(tapes == null || tapes.size() == 0){
            setAllChildGone();
        }
        int tapeCount = tapes.size();
        int childCount = getChildCount();
        if(mCallBack == null){
            mCallBack = genCallback();
        }
        for(int i = 0; i < tapeCount; i++){
            if(i < childCount){
                View view = getChildAt(i);
                if(view instanceof TapeMessageView){
                    ((TapeMessageView)view).update(tapes.get(i));
                    view.setVisibility(View.VISIBLE);
                }
            }else{
                TapeMessageView view = TapeViewUtil.genTapeMessageView(getContext(), tapes, i, mCallBack);
                addView(view);
            }
        }
        if(tapeCount < childCount){
            for(int i = tapeCount; i < childCount; i++){
                getChildAt(i).setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void addView(View child) {
        if(child != null){
            if(!(child instanceof TapeMessageView)){
                throw new IllegalArgumentException("view in TapeMessageContainerView should be instance of class TapeMessageView");
            }
        }
        super.addView(child);
    }

    private void setAllChildGone(){
        int childCount = getChildCount();
        for(int i = 0; i < childCount; i++){
            getChildAt(i).setVisibility(View.GONE);
        }
    }

    private TapeMessageView.TapeOperate genCallback(){
//        if(mRecorderWrapper == null) {
//            mRecorderWrapper = new RecorderWrapper(getContext().getApplicationContext(), RecorderManager.RECORDER_DIR);
//        }
        mRecorderWrapper = RecorderWrapper.getInstance();
        TapeMessageView.TapeOperate callback = new TapeMessageView.TapeOperate() {
            @Override
            public void onTapeToPlay(TapeMessageView view, BeanCustomerTape tape, float progress) {
                mRecorderWrapper.playRecordFile(tape.getFilePath());
            }
            @Override
            public void onTapeToPause(TapeMessageView view, BeanCustomerTape tape, float progress) {
                mRecorderWrapper.pausePlayRecordFile();
            }
        };
        return callback;
    }
}